﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace UnityDependencyResolver.Lib
{
  public class UnityMVCDependencyResolver : IDependencyResolver
  {
    private readonly IUnityContainer _container;

    public UnityMVCDependencyResolver(IUnityContainer container)
    {
      _container = container;
    }

    public object GetService(Type serviceType)
    {
      if (typeof(IController).IsAssignableFrom(serviceType))
      {
        return _container.Resolve(serviceType);
      }

      if (_container.IsRegistered(serviceType))
        return _container.Resolve(serviceType);
      else
        return null;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
      return _container.ResolveAll(serviceType);
    }

  }
}